package entity_controllers.options;

import misc.MArrayList;
import core.subcontrollers.PlayerController;
import core.subcontrollers.SubController;
import boundary.UI;
import ie.FieldBoard;
import ie.PlayerBoard;
import ie.Shaker;
import dto.Player;
import entity.Account;
import entity.fields.properties.Street;
import entity_controllers.Option;

public class ShakeMoveOption extends Option {

	private int diceEqualTimes = 0;
	
	public ShakeMoveOption(String title) {
		super(title);
	}

	@Override
	public void execute(Player player, FieldBoard fieldBoard, PlayerBoard playerBoard, Shaker shaker) {
		shaker.shake();
		UI.get().showDice(shaker.getDices());
		
		boolean isEqual = shaker.getDices()[0].getValue() == shaker.getDices()[1].getValue();

		if(isEqual)
			diceEqualTimes++;
		else
			diceEqualTimes = 0;
		
		if(diceEqualTimes == 3){
			UI.get().getMessage("Du har slået 2 ens 3 gange i træk, gå i fængsel!");

			PlayerController.setInJail(player, fieldBoard);
		}
		else{
			int diceSum = shaker.getDiceSum();
			player.setDiceSum(diceSum);
			
			PlayerController.movePlayerByDiceSum(player, fieldBoard, playerBoard, shaker);
			
			if(diceEqualTimes == 0){
				this.disable();
				if (SubController.streetConstruction(player, fieldBoard, new MArrayList<Street>()))
					((Account) player.getAccount()).getOM().getOption(BuyHouseOption.class).enable();
				if(!((Account) player.getAccount()).isBankrupt())
					((Account) player.getAccount()).getOM().getOption(EndTurnOption.class).enable();
			}
			
		}

	}

	@Override
	public void reset() {}

}
